﻿/*
 * Copyright 2008 Lucas Tan.
 * This copyright notice must always remain intact whenever you use or distribute 
 * the source code contained in this file.
 * Any usage of any code in this file, either in derived work or
 * through the use of external linkage,
 * must always be credited with the author's name.
 * 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 * 
 * */
using System;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace PassKeeper.Properties 
{
    // This class allows you to handle specific events on the settings class:
    //  The SettingChanging event is raised before a setting's value is changed.
    //  The PropertyChanged event is raised after a setting's value is changed.
    //  The SettingsLoaded event is raised after the setting values are loaded.
    //  The SettingsSaving event is raised before the setting values are saved.
    internal sealed partial class Settings 
	{
		/*Remember to change the min/max length in the Password Validator regex
		 if the values below are changed.
		 Also change those label text that refer to the lengths.
		 */
		public const int DatabasePasswordMinLength = 5;
		public const int DatabasePasswordMaxLength = 20;

		public static readonly Regex  DatabasePasswordValidator =
			new Regex(
				"^[a-z0-9_\\-]{5,20}$",
				RegexOptions.CultureInvariant |
				RegexOptions.IgnoreCase |
				RegexOptions.Singleline
			);

        public Settings() 
		{
            // // To add event handlers for saving and changing settings, uncomment the lines below:
            //
            // this.SettingChanging += this.SettingChangingEventHandler;
            //
            // this.SettingsSaving += this.SettingsSavingEventHandler;
            //
        }

		/// <summary>
		/// Gets the absolute path of the default database file name.
		/// </summary>
		/// <value>The return value can be null to indicate 
		/// there is no default database file name or the file name specified
		/// in the settings file is invalid.</value>
		public string DefaultDatabaseFullPath
		{
			get
			{
				
				try
				{
					string fileName = this.DefaultDatabaseFileName;

					if (string.IsNullOrEmpty(fileName))
					{
						return null;
					}

					if (Path.IsPathRooted(fileName))
					{
						/*'fileName' is an absolute path, so return it.*/
						return fileName;
					}
					else
					{
						/*'fileName' is a relative path.. so combine it with
						 the application path.*/

						return Path.Combine(Application.StartupPath, fileName);
					}
				}
				catch
				{
					/*Assume 'fileName' is invalid and return null*/
					return null;
				}
			}
		}

		private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e)
		{
			// Add code to handle the SettingChangingEvent event here.
		}

		private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e)
		{
			// Add code to handle the SettingsSaving event here.
		}
    }
}
